package mn.slide.dailynews.tv.xml.parser;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import mn.slide.dailynews.helper.DatabaseHelper;
import mn.slide.dailynews.model.Tv;
import mn.slide.dailynews.model.TvDay;
import mn.slide.dailynews.model.TvDayItem;
import mn.slide.dailynews.utils.ImageTool;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.content.Context;

import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;

public class SaxHandler extends DefaultHandler
{
	private static final String		TAG				= "SAXHANDLER";
	private Context					context;
	private DatabaseHelper			databaseHelper	= null;
	private Dao<Tv, Integer>		tvDao;
	private Dao<TvDay, Integer>		tvDayDao;
	private Dao<TvDayItem, Integer>	tvDayItemDao;
	private Tv						tv;
	private TvDay					tvDay;

	public SaxHandler(Context context)
	{
		this.context = context;
		try
		{
			tvDao = getHelper().getTvDao();
			tvDayDao = getHelper().getTvDayDao();
			tvDayItemDao = getHelper().getTvDayItemDao();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
	}

	public void startDocument()
	{
	}

	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
	{
		if ( qName.equalsIgnoreCase("tv") )
		{
			String tvName = attributes.getValue("name");
			String imageUrl = attributes.getValue("image");
			try
			{
				QueryBuilder<Tv, Integer> queryBuilder = tvDao.queryBuilder();
				queryBuilder.where().eq(Tv.NAME_FIELD_NAME, tvName);
				PreparedQuery<Tv> preparedQuery = queryBuilder.prepare();
				List<Tv> tvs = tvDao.query(preparedQuery);
				if ( tvs == null || tvs.size() == 0 )
				{
					tv = new Tv(tvName,ImageTool.getDataByteArrayFromUrl(imageUrl));
					tvDao.create(tv);
				}
				else
				{
					tv = tvs.get(0);
				}
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		else if ( qName.equalsIgnoreCase("day") )
		{
			try
			{
				String dayName = attributes.getValue("name");
				Date date = new Date(attributes.getValue("date"));
				QueryBuilder<TvDay, Integer> queryBuilder = tvDayDao.queryBuilder();
				Where<TvDay, Integer> where = queryBuilder.where();
				where.eq(TvDay.NAME_FIELD_NAME, dayName);
				where.and();
				where.eq(TvDay.TV_FIELD_NAME, tv);
				where.and();
				where.eq(TvDay.DATE_FIELD_NAME, date);
				PreparedQuery<TvDay> preparedQuery = queryBuilder.prepare();
				List<TvDay> days = tvDayDao.query(preparedQuery);
				if ( days == null || days.size() == 0 )
				{
					tvDay = new TvDay(dayName, date, tv);
					tvDayDao.create(tvDay);
				}
				else
				{
					tvDay = days.get(0);
				}
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		else if ( qName.equalsIgnoreCase("item") )
		{
			try
			{
				String name = attributes.getValue("name");
				Date startdate = new Date(attributes.getValue("start"));
				Date enddate = new Date(attributes.getValue("end"));
				QueryBuilder<TvDayItem, Integer> queryBuilder = tvDayItemDao.queryBuilder();
				Where<TvDayItem, Integer> where = queryBuilder.where();
				where.eq(TvDayItem.TVDAY_FIELD_NAME, tvDay);
				where.and();
				where.eq(TvDayItem.START_DATE_FIELD_NAME, startdate);
				where.and();
				where.eq(TvDayItem.END_DATE_FIELD_NAME, enddate);
				PreparedQuery<TvDayItem> preparedQuery = queryBuilder.prepare();
				List<TvDayItem> tvDayItems = tvDayItemDao.query(preparedQuery);
				if ( tvDayItems == null || tvDayItems.size() == 0 )
				{
					TvDayItem tvDayItem = new TvDayItem(name, startdate, enddate, tvDay);
					tvDayItemDao.create(tvDayItem);
				}
				else
				{
					TvDayItem tvDayItem = tvDayItems.get(0);
					tvDayItem.name = name;
					tvDayItem.setStartDate(startdate);
					tvDayItem.setEndDate(enddate);
					tvDayItemDao.update(tvDayItem);
				}
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}

	public void characters(char ch[], int start, int length) throws SAXException
	{
	}

	public void endElement(String uri, String localName, String qName) throws SAXException
	{
	}

	public void endDocument()
	{
	}

	public Tv getTv()
	{
		return tv;
	}

	public void setTv(Tv tv)
	{
		this.tv = tv;
	}

	public TvDay getTvDay()
	{
		return tvDay;
	}

	public void setTvDay(TvDay tvDay)
	{
		this.tvDay = tvDay;
	}

	public DatabaseHelper getHelper()
	{
		if ( databaseHelper == null )
			databaseHelper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
		return databaseHelper;
	}

	public static String getTag()
	{
		return TAG;
	}
}